/*
 * @Author: 0x9DEFA478
 * @Date: 2024-08-02 20:00:34
 * @LastEditTime: 2024-08-09 22:28:36
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "./H_TS_Semaphore.h"
#include "../LL/H_TS_LL.h"




Husize H_TS_Semaphore_TryRelease_ISR(H_TS_Semaphore* Semaphore){
  Husize MaxValue;
  Husize OldValue;

  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_TryRelease_ISR():空的句柄");

  MaxValue=Semaphore->MaxValue;

  OldValue=H_TS_LL_AtomicAccess_ValueIncrement(&Semaphore->Value,MaxValue);

  if(OldValue>=MaxValue){//值维持在最大值 直接返回
    return OldValue;
  }

  H_TS_Event_Triggering_ISR(&Semaphore->ReleaseEvent);

  return OldValue;
}

Husize H_TS_Semaphore_TryRelease(H_TS_Semaphore* Semaphore){
  Husize MaxValue;
  Husize OldValue;
  
  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_TryRelease():空的句柄");

  MaxValue=Semaphore->MaxValue;

  OldValue=H_TS_LL_AtomicAccess_ValueIncrement(&Semaphore->Value,MaxValue);

  if(OldValue<MaxValue){
    H_TS_Event_Triggering(&Semaphore->ReleaseEvent);
  }
  return OldValue;
}

Husize H_TS_Semaphore_Release(H_TS_Semaphore* Semaphore){
  Husize MaxValue;
  Husize OldValue;

  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_Release():空的句柄");

  MaxValue=Semaphore->MaxValue;

  while((OldValue=H_TS_LL_AtomicAccess_ValueIncrement(&Semaphore->Value,MaxValue))>=MaxValue){
    H_TS_Lock_Lock(&Semaphore->ReleaseLock);
    H_TS_Event_Take(&Semaphore->AcquireEvent);
    H_TS_Lock_Unlock(&Semaphore->ReleaseLock);
  }

  H_TS_Event_Triggering(&Semaphore->ReleaseEvent);
  return OldValue;
}

Husize H_TS_Semaphore_Release_Time(H_TS_Semaphore* Semaphore,H_TS_Tick Timeout){
  Husize MaxValue;
  Husize OldValue;
  H_TS_Tick DeadLine;

  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_Release_Time():空的句柄");
  oH_TS_Assert_ParamValid(Timeout<0,"H_TS:H_TS_Semaphore_Release_Time():Timeout小于0");

  if(Timeout<=0){
    return H_TS_Semaphore_TryRelease(Semaphore);
  }
  DeadLine=H_TS_LL_GetRunningTick()+Timeout;

  MaxValue=Semaphore->MaxValue;

  while((OldValue=H_TS_LL_AtomicAccess_ValueIncrement(&Semaphore->Value,MaxValue))>=MaxValue){
    H_TS_Tick timeout;
    timeout=DeadLine-H_TS_LL_GetRunningTick();
    if(timeout<=0){
      break;
    }
    H_TS_Lock_Lock(&Semaphore->ReleaseLock);
    H_TS_Event_Take_Time(&Semaphore->AcquireEvent,timeout);
    H_TS_Lock_Unlock(&Semaphore->ReleaseLock);
  }

  if(OldValue<MaxValue){
    H_TS_Event_Triggering(&Semaphore->ReleaseEvent);
  }
  return OldValue;
}

Husize H_TS_Semaphore_TryAcquire(H_TS_Semaphore* Semaphore){
  Husize OldValue;

  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_TryAcquire():空的句柄");

  if((OldValue=H_TS_LL_AtomicAccess_ValueDecrement(&Semaphore->Value))<=0){
    return OldValue;
  }
  
  H_TS_Event_Triggering(&Semaphore->AcquireEvent);
  return OldValue;
}

Husize H_TS_Semaphore_Acquire(H_TS_Semaphore* Semaphore){
  Husize OldValue;

  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_Acquire():空的句柄");

  while((OldValue=H_TS_LL_AtomicAccess_ValueDecrement(&Semaphore->Value))<=0){
    H_TS_Lock_Lock(&Semaphore->AcquireLock);
    H_TS_Event_Take(&Semaphore->ReleaseEvent);
    H_TS_Lock_Unlock(&Semaphore->AcquireLock);
  }

  H_TS_Event_Triggering(&Semaphore->AcquireEvent);
  return OldValue;
}

Husize H_TS_Semaphore_Acquire_Time(H_TS_Semaphore* Semaphore,H_TS_Tick Timeout){
  Husize OldValue;
  H_TS_Tick DeadLine;

  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_Acquire_Time():空的句柄");
  oH_TS_Assert_ParamValid(Timeout<0,"H_TS:H_TS_Semaphore_Acquire_Time():Timeout小于0");

  if(Timeout<=0){
    return H_TS_Semaphore_TryAcquire(Semaphore);
  }
  DeadLine=H_TS_LL_GetRunningTick()+Timeout;

  while((OldValue=H_TS_LL_AtomicAccess_ValueDecrement(&Semaphore->Value))<=0){
    H_TS_Tick timeout;
    timeout=DeadLine-H_TS_LL_GetRunningTick();
    if(timeout<=0){
      break;
    }
    H_TS_Lock_Lock(&Semaphore->AcquireLock);
    H_TS_Event_Take_Time(&Semaphore->ReleaseEvent,timeout);
    H_TS_Lock_Unlock(&Semaphore->AcquireLock);
  }

  if(OldValue>0){
    H_TS_Event_Triggering(&Semaphore->AcquireEvent);
  }
  return OldValue;
}

void H_TS_Semaphore_ReleaseLock(H_TS_Semaphore* Semaphore){
  H_TS_Lock_Lock(&Semaphore->ReleaseLock);
}

void H_TS_Semaphore_ReleaseUnlock(H_TS_Semaphore* Semaphore){
  H_TS_Lock_Unlock(&Semaphore->ReleaseLock);
}

void H_TS_Semaphore_AcquireLock(H_TS_Semaphore* Semaphore){
  H_TS_Lock_Lock(&Semaphore->AcquireLock);
}

void H_TS_Semaphore_AcquireUnlock(H_TS_Semaphore* Semaphore){
  H_TS_Lock_Unlock(&Semaphore->AcquireLock);
}



